/*
 * @Author: hx 2542123711@QQ.com
 * @Date: 2025-01-11 19:24:42
 * @LastEditors: hx 2542123711@QQ.com
 * @LastEditTime: 2025-01-11 19:47:07
 * @FilePath: /class_socket/tcp_socket/Init.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include<iostream>
#include<fstream>
#include<unordered_map>
#include<cstring>
#include<string>
#include"Log.hpp"

using namespace std;
const string dictname = "./dict.txt";
const string sep = ":";
extern Log log;

bool Split(const string& line, string& part1, string& part2)
{
    auto pos = line.find(sep);
    if(pos == std::string::npos)
    {
        return false;
    }

    part1 = line.substr(0, pos);
    part2 = line.substr(pos+1);

    return true;
}

class Init
{
public:
    Init()
    {
        ifstream in(dictname);
        if(!in.is_open())
        {
            log(Fatal, "ifstream %s open error", dictname.c_str());
            exit(1);
        }

        string line;
        while(getline(in, line))
        {
            string part1;
            string part2;

            Split(line, part1, part2);
            _dict.insert({part1, part2});
        }

    }

    string translate(const string& key)
    {
        auto iter = _dict.find(key);
        if(iter == _dict.end())
        {
            return "NONE";
        }
        
        return iter->second;
    }

private:
    unordered_map<string, string> _dict;
};